/**
 * Created by Administrator on 2017/8/24.
 */
let variable = require('../data/variable.js');
let util = require('../util/util.js');
let log = require('../util/log.js');
let net = require('net');
let fs = require('fs');
let cp = require('child_process');

module.exports = app;

function app(dirName) {
    this._dirName = dirName;
    this._process = {};
}
app.prototype.start = function () {
    let _this = this;
    this.server();
    process.on('SIGINT', function () {
        return false;
        //log.p('wait 5 min close server')
        //setTimeout(function () {
        //    _this.exit();
        //}, 5000);
    });
};
app.prototype.server = function () {
    let ipv4 = util.getLocalIPv4(); 
    console.log(ipv4);
    //let server = ['http', 'dc', 'enter', 'func', 'match', 'game', 'activity', 'arena', 'gate'];
    let server = ['http', 'dc', 'enter', 'func', 'match', 'game', 'activity', 'gate'];
    for (let i in server) { 
        if (variable[server[i]].id) {   
            if (ipv4 == variable[server[i]].local) { 
                this.process(server[i]);
            }
        } else {
            for (let j in variable[server[i]]) {  
                if (ipv4 == variable[server[i]][j].local) { 
                    this.process(server[i], j);
                }
            }
        }
    }
};

/**
 * 创建一个子进程，根据mode来决定模式
 * @param type
 * @param i
 */
app.prototype.process = function (type, i) {
    if (!i) {
        i = 0;
    }
    //this.processExec(type, i);
    this.processSpawn(type, i);
};

/**
 * 用exec模式启动子进程
 * @param type
 * @param i
 */
app.prototype.processExec = function (type, i) {
    let _this = this;
    let server = cp.exec('node ' + this._dirName + '/app/' + type + '/index.js ' + i);
    log.p('server ' + type + '\t' + 'pid ' + server.pid);
    _this._process[server.pid] = server;

    server.stdout.on('data', function (data) {
        let txt = data.toString();
        txt = txt.substring(0, txt.length - 1);
        console.log(txt);
    });
    server.stderr.on('data', function (data) {
        let txt = data.toString();
        txt = txt.substring(0, txt.length - 1);
        console.error(txt);
    });
    server.on('close', function (code, signal) {
        server.kill(signal);
        setTimeout(function () {
            _this.processExec(type, i);
        }, 5000);
    });
    server.on('error', function (code, signal) {
        server.kill(signal);
        setTimeout(function () {
            _this.processExec(type, i);
        }, 5000);
    });
    server.on('exit', function (code, signal) {
        _this.kill(server, code, signal);
    });
};

/**
 * 用spawn模式启动子进程
 * @param type
 * @param i
 */
app.prototype.processSpawn = function (type, i) {
    let _this = this;
    let parameter = [];
    parameter.push(this._dirName + '/app/' + type + '/index.js');
    parameter.push(i);
    let server = cp.spawn('node', parameter);
    log.p('server ' + type + '\t' + 'pid ' + server.pid);
    _this._process[server.pid] = server;
    server.stdout.on('data', function (data) {
        let txt = data.toString();
        txt = txt.substring(0, txt.length - 1);
        console.log(txt);
    });
    server.stderr.on('data', function (data) {
        let txt = data.toString();
        txt = txt.substring(0, txt.length - 1);
        console.error(txt);
    });
    server.on('close', function (code, signal) {
        server.kill(signal);
        setTimeout(function () {
            _this.processSpawn(type, i);
        }, 5000);
    });
    server.on('error', function (code, signal) {
        server.kill(signal);
        setTimeout(function () {
            _this.processSpawn(type, i);
        }, 5000);
    });
    server.on('exit', function (code, signal) {
        _this.kill(server, code, signal);
    });
};
app.prototype.exit = function () {
    for (let i in this._process) {
        this._process[i].kill();
    }
};
app.prototype.kill = function (server, code, signal) {
    console.log('exit', code, signal);
    log.p('process exit', server.pid, code);
    //server.kill(signal);
    delete this._process[server.pid];
    if (Object.keys(this._process).length <= 0) {
        log.p('master process exit');
        process.exit();
    }
    console.log('server ' + server.pid + ' exit');
};